<?php

namespace app\youzhi\controller;


use app\admin\model\BdDatacenter;
use app\admin\model\BdOrg;
use app\admin\model\Synlogs;
use app\erp\controller\Bizperson;
use app\erp\controller\BizpersonCate;
use app\erp\controller\Carinfo;
use app\erp\controller\CarinfoCate;
use app\erp\controller\Customer;
use app\erp\controller\CustomerCate;
use app\erp\controller\Inventory;
use app\erp\controller\Mater;
use app\erp\controller\MaterialCate;
use app\erp\controller\Matprop;
use app\erp\controller\Payrecord;
use app\erp\controller\Payrecordno;
use app\erp\controller\Purorder;
use app\erp\controller\Surface;
use app\erp\controller\SurfaceCate;
use app\erp\controller\Texture;
use app\erp\controller\TextureCate;
use think\Controller;
use think\Env;
use think\Log;

class Erp extends Controller
{
    protected $client_id    = "bdl_WeChat";
    protected $user_name;
    protected $starttime;
    protected $endtime;
    protected $pageSize     = 1000;
    protected $action;

    public function __construct()
    {
        parent::__construct();

        ini_set('memory_limit','4096M');
        set_time_limit(0);

        $time               = $this->getTime();
        $this->starttime    = $time['startTime'];
        $this->endtime      = $time['endTime'];
        $this->user_name    = Env::get('erp.user_name');
        $this->action = [
//            'MaterialCate'                          => '产品分组',
//            'matprop'                               => '属性',
//            'material'                              => '物料',
//
            'bizpersoncate'                         => '业务分组',
            'bizperson'                             => '业务员',
//
            'texturecate'                           => '材质组',
            'texture'                               => '材质',
            'surfacecate'                           => '表面组',
            'surface'                               => '表面',
            'carinfocate'                           => '配送组',
            'carinfo'                               => '配送车',
            'curstomercate'                         => '客户组',
            'curstomer'                             => '客户',
//
            'ivnentory'                             => '库存',
            'purorder'                              => 'ERP订单',
            'payrecord'                             => '付款记录（已确认）',
            'payrecordno'                           => '付款记录（未通过）',
        ];
    }

    public function run($type,$datacenter = null)
    {
        $map  = [];
        if(!empty($datacenter))
        {
         $map['fnumber'] = ['=',$datacenter];
        }
        $data = BdDatacenter::whereNotNull('fk_bdl_clientsecret')
            ->where('fk_bdl_clientsecret','neq',' ')
            ->where('fnumber',"not in",[1842944793479483392,1833526278980894720,1808778350425014272,1950307912941305856])
            ->where($map)
            ->select();

        foreach ($data as $k=>$v)
        {
            if(empty($v['fk_bdl_clientsecret']))
            {
                continue;
            }
            $forgs = BdOrg::where('fk_bdl_bd_datacenter',$v['fnumber'])
                ->select();

//            pdump([$type,$this->starttime, $this->endtime, $this->client_id,$v['fk_bdl_clientsecret'],$v['fnumber'],$this->user_name]);
            $res    = $this->erp($type,$this->starttime, $this->endtime, $this->client_id,$v['fk_bdl_clientsecret'],$v['fnumber'],$this->user_name);


            $count  = [];


            foreach ($forgs as $key=>$value)
            {
                $uuid = Synlogs::create([
                    'forgid'                => $value['id'],
                    'fk_bdl_bd_datacenter'  => $value['fk_bdl_bd_datacenter'],
                    'fk_bdl_username'       => !empty(session('admin')['nickname']) ? session('admin')['nickname'] : "系统自动执行",
                    'fk_bdl_calltime'       => date('Y-m-d H:i:s'),
                    'fk_bdl_executetype'    => 2,
                    'fk_bdl_callmethod'     => $this->action[$type]
                ])->id;
                $run_res = $res->run($value['fnumber'],$value['id'],1,$this->pageSize,$uuid);

                if($run_res === 1)
                {
                    $msg = "组织编码：{$value['fnumber']},组织名称：{$value['fname']},同步开始时间{$this->starttime},同步结束时间{$this->endtime}" . $type . "同步成功";
                    Log::info($msg);
                }

                $count[] = $run_res;
            }

            if(count($count) ===  count($forgs))
            {
                $msg = "数据中心编码：{$v['fnumber']},数据中心名称：{$v['fname']}.同步开始时间{$this->starttime},同步结束时间{$this->endtime}" . $type . "同步成功";
                Log::info($msg);
            }

        }

        return true;
    }

    public function erp($type,$starttime, $endtime,$client_id,$client_secret,$account_id,$user_name)
    {
        switch ($type)
        {
//            case "MaterialCate":
//                $erp = new MaterialCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
//                break;
//            case "matprop":
//                $erp = new Matprop($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
//                break;
//            case "material":
//                $erp = new Mater($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
//                break;

            case "bizpersoncate":
                $erp = new BizpersonCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "bizperson":
                $erp = new Bizperson($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "carinfo":
                $erp = new Carinfo($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "carinfocate":
                $erp = new CarinfoCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "texturecate":
                $erp = new TextureCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "texture":
                $erp = new Texture($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "surfacecate":
                $erp = new SurfaceCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "surface":
                $erp = new Surface($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "curstomercate":
                $erp = new CustomerCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "curstomer":
                $erp = new Customer($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;

            case "ivnentory":
                $erp = new Inventory($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;

            case "purorder":
                $erp = new Purorder($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "payrecord":
                $erp = new Payrecord($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "payrecordno":
                $erp = new Payrecordno($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;


        }

        return $erp;
    }
    public function getTime()
    {
        $now = time();

        $startTime = strtotime('-30 minutes', $now);

        $endTime = strtotime('now', $now);

        $startTimeFormatted = date('Y-m-d H:i:s', $startTime);
        $endTimeFormatted = date('Y-m-d H:i:s', $endTime);

        return [
            'startTime' => $startTimeFormatted,
            'endTime' => $endTimeFormatted,
        ];
    }


}